/*
 * WelfareMgr.cpp
 *
 *  Created on: Nov 29, 2017
 *      Author: gaoyi
 */

#include "OperateWelfareMgr.h"
#include "Common/TableData/OperateHuodongCfg.h"
#include "Common/TableData/OperateJiangliCfg.h"
#include "Common/TableData/OperateDuihuanCfg.h"
#include "Common/Utility/TimeUtility.h"
#include "Common/lexical_cast.hpp"
#include "Common/Utility/ServerTime.h"

void OperateWelfareMgr::OnTimer(uint32_t nTimerID)
{
	uint32_t actType = nTimerID / 10000;
	bool isStartTimer = (nTimerID % 10000 == 1) ? true : false;
	OperateActConfigItem* pConfig = GetWelfareConfigItemFromActType(actType);
	if (pConfig)
	{
		if (isStartTimer)
		{
			pConfig->SetIsOpen(true);
		}
		else
		{
			pConfig->SetIsOpen(false);
		}
	}
}

bool OperateWelfareMgr::Init()
{
	uint64_t curTime = g_GetGlobalServerTime()->UnixSec();
	const OperateHuodongCfgMap* pActCfgMap = g_GetOperateHuodongCfgTable()->GetOperateHuodongCfgMap();
	for (auto it = pActCfgMap->begin(); it != pActCfgMap->end(); it++)
	{
		const OperateHuodongCfgInfo& cfgInfo = it->second;
		if (mConfigMap.find(cfgInfo.huoDongId) == mConfigMap.end())
		{
			OperateActConfigItem *ActItem = new OperateActConfigItem();
			ActItem->SetActId(cfgInfo.huoDongId);
			ActItem->SetActType(cfgInfo.nameId);
			ActItem->SetIsOpen(cfgInfo.ifOpen);

			std::string strBeginTime = cfgInfo.timeA;
			std::string strEndTime = cfgInfo.timeB;
			CommonApi::StrTrim(strBeginTime);
			CommonApi::StrTrim(strEndTime);

			if (!strBeginTime.empty() && !strEndTime.empty())
			{
				strBeginTime += ":00";
				strEndTime += ":00";
				int32_t beginTime = (int32_t)TimeUtility::LocalTimeStampToUnixSec("%d/%d/%d/%d:%d:%d", strBeginTime.c_str());
				int32_t endTime = (int32_t)TimeUtility::LocalTimeStampToUnixSec("%d/%d/%d/%d:%d:%d", strEndTime.c_str());

				if (beginTime > 0 && endTime > 0 && beginTime < endTime)
				{
					ActItem->SetIsTimeLimit(true);
					ActItem->GetActTime().GetTime().SetMinValue(beginTime);
					ActItem->GetActTime().GetTime().SetMaxValue(endTime);
					ActItem->GetActTime().GetTime().SetBIsValid(true);

					if (curTime < ((uint64_t)beginTime))
					{
						ActItem->SetIsOpen(false);
						int timeId = ActItem->GetActType() * 10000 + 1;
						uint64_t iterval = ((uint64_t)beginTime) - curTime;
						g_GetTimerAxis()->SetTimer(timeId, iterval*1000, dynamic_cast<ITimerHandler*>(this), 1);
					}

					if (curTime < ((uint64_t)endTime))
					{
						int timeId = ActItem->GetActType() * 10000 + 2;
						uint64_t iterval = ((uint64_t)endTime) - curTime;
						g_GetTimerAxis()->SetTimer(timeId, iterval*1000, dynamic_cast<ITimerHandler*>(this), 1);
					}

					if (curTime > ((uint64_t)endTime))
					{
						ActItem->SetIsOpen(false);
					}
				}
				else
				{
					ActItem->SetIsTimeLimit(false);
				}
			}
			else
			{
				ActItem->SetIsTimeLimit(false);
			}

			std::string condStr = cfgInfo.tiaoJian;
			CommonApi::StrTrim(condStr);
			std::vector<int> vecId;
			CommonApi::SplitStrToVecInt(condStr, ",", &vecId);
			for(int i = 0; i < (int)vecId.size(); i++)
			{
				int itemId = vecId[i];
				const OperateJiangliCfgInfo *pJiangliCfgInfo = g_GetOperateJiangliCfgTable()->GetOperateJiangliCfgInfo(itemId);
				if (pJiangliCfgInfo == nullptr)
				{
					continue;
				}

				WelfareConfigItem* condItem = new WelfareConfigItem();
				condItem->SetItemId(itemId);
				condItem->SetItemType(ActItem->GetActType());

				{
					condItem->GetActCondition().GetFaculty().SetMinValue(pJiangliCfgInfo->downNu);
					condItem->GetActCondition().GetFaculty().SetMaxValue(pJiangliCfgInfo->upNu);
				}

				condItem->GetActCondition().GetFaculty().SetBIsValid(true);
				condItem->SetRewardId(pJiangliCfgInfo->jiangLi);
				condItem->SetTiaoJian(pJiangliCfgInfo->tiaoJian);
				condItem->SetExtBoxId(pJiangliCfgInfo->extBoxId);
				condItem->SetSonId(pJiangliCfgInfo->sonId);

				ActItem->GetVecWelfareItem().push_back(condItem);

				if (pJiangliCfgInfo->rechargeId > 0)
				{
					mMapRechargeIdActIdItemId[pJiangliCfgInfo->rechargeId] = std::pair<uint32_t, uint32_t>(cfgInfo.huoDongId, itemId);
				}
			}
			mConfigMap.emplace(ActItem->GetActId(), ActItem);
		}
	}

	const OperateDuihuanCfgMap *pDuihuanMap = g_GetOperateDuihuanCfgTable()->GetOperateDuihuanCfgMap();
	if (pDuihuanMap)
	{
		for(auto iter = pDuihuanMap->begin(); iter != pDuihuanMap->end(); iter++)
		{
			const OperateDuihuanCfgInfo* pCfgInfo = &iter->second;
			ExchangeRewardCfg rewardCfg;
			rewardCfg.SetIndexId(pCfgInfo->id);
			rewardCfg.SetRewardPropId(pCfgInfo->itemId);
			rewardCfg.SetRewardPropNum(pCfgInfo->prizeNum);
			rewardCfg.SetMaxUseCount(pCfgInfo->times);

			for(int i = 0; i < (int)pCfgInfo->vecOperateDuihuanPropCfg.size(); i++)
			{
				if (pCfgInfo->vecOperateDuihuanPropCfg[i].Id > 0 && pCfgInfo->vecOperateDuihuanPropCfg[i].Num > 0)
				{
					AddSpecialExchangeItemId(pCfgInfo->vecOperateDuihuanPropCfg[i].Id);
					ExchangeItemCfg itemCfg;
					itemCfg.SetPropId(pCfgInfo->vecOperateDuihuanPropCfg[i].Id);
					itemCfg.SetPropNum(pCfgInfo->vecOperateDuihuanPropCfg[i].Num);
					rewardCfg.GetVecExchangeItemCfg().push_back(itemCfg);
				}
			}
			mMapExchangeRewardCfg.emplace(rewardCfg.GetIndexId(), rewardCfg);
		}
	}

	//����Ŀ��
	{
		const TargetNormalCfgMap *pNormalMap = g_GetTargetNormalCfgTable()->GetTargetNormalCfgMap();
		if (nullptr != pNormalMap)
		{
			for (TargetNormalCfgMap::const_iterator iter = pNormalMap->begin(); iter != pNormalMap->end(); ++iter)
			{
				const TargetTargetCfgInfo *pTargetCfg = g_GetTargetTargetCfgTable()->GetTargetTargetCfgInfo(iter->second.targetID);
				if (nullptr != pTargetCfg)
				{
					mGoal7DaysMap[pTargetCfg->targetType][iter->first] = Goals7DaysData();
					mGoal7DaysMap[pTargetCfg->targetType][iter->first].pCfg = pTargetCfg;
					mGoal7Daysprogress++;
				}
				else
					LogErrFmtPrint("[logic] OperateWelfareMgr::Init...nullptr == pTargetCfg...fail...targetId=%d", iter->second.targetID);
			}
		}
		else
			LogErrFmtPrint("[logic] OperateWelfareMgr::Init...nullptr == pNormalMap...fail");

		const TargetLoginCfgMap *pLoginMap = g_GetTargetLoginCfgTable()->GetTargetLoginCfgMap();
		if (nullptr != pLoginMap)
		{
			for (TargetLoginCfgMap::const_iterator iter = pLoginMap->begin(); iter != pLoginMap->end(); ++iter)
			{
				const TargetTargetCfgInfo *pTargetCfg = g_GetTargetTargetCfgTable()->GetTargetTargetCfgInfo(iter->second.targetID);
				if (nullptr != pTargetCfg)
				{
					mGoal7DaysMap[pTargetCfg->targetType][iter->first] = Goals7DaysData();
					mGoal7DaysMap[pTargetCfg->targetType][iter->first].pCfg = pTargetCfg;
					mGoal7Daysprogress++;
				}
				else
					LogErrFmtPrint("[logic] OperateWelfareMgr::Init...nullptr == pLoginCfg...fail...targetId=%d", iter->second.targetID);
			}
		}
		else
			LogErrFmtPrint("[logic] OperateWelfareMgr::Init...nullptr == pLoginMap...fail");

		const TargetSaleCfgMap *pSaleMap = g_GetTargetSaleCfgTable()->GetTargetSaleCfgMap();
		if (nullptr != pSaleMap)
		{
			for (TargetSaleCfgMap::const_iterator iter = pSaleMap->begin(); iter != pSaleMap->end(); ++iter)
			{
				const TargetTargetCfgInfo *pTargetCfg = g_GetTargetTargetCfgTable()->GetTargetTargetCfgInfo(iter->second.targetID);
				if (nullptr != pTargetCfg)
				{
					mGoal7DaysMap[pTargetCfg->targetType][iter->first] = Goals7DaysData();
					mGoal7DaysMap[pTargetCfg->targetType][iter->first].pCfg = pTargetCfg;
					mGoal7Daysprogress++;
				}
				else
					LogErrFmtPrint("[logic] OperateWelfareMgr::Init...nullptr == pSaleMap...fail...targetId=%d", iter->second.targetID);
			}
		}
		else
			LogErrFmtPrint("[logic] OperateWelfareMgr::Init...nullptr == pLoginMap...fail");

		const TargetDuplicateCfgMap *pDuplicateMap = g_GetTargetDuplicateCfgTable()->GetTargetDuplicateCfgMap();
		if (nullptr != pDuplicateMap)
		{
			for (TargetDuplicateCfgMap::const_iterator iter = pDuplicateMap->begin(); iter != pDuplicateMap->end(); ++iter)
			{
				const TargetTargetCfgInfo *pTargetCfg = g_GetTargetTargetCfgTable()->GetTargetTargetCfgInfo(iter->second.targetID);
				if (nullptr != pTargetCfg)
				{
					mGoal7DaysMap[pTargetCfg->targetType][iter->first] = Goals7DaysData();
					mGoal7DaysMap[pTargetCfg->targetType][iter->first].pCfg = pTargetCfg;
					mGoal7Daysprogress++;
				}
				else
					LogErrFmtPrint("[logic] OperateWelfareMgr::Init...nullptr == pDuplicateMap...fail...targetId=%d", iter->second.targetID);
			}
		}
		else
			LogErrFmtPrint("[logic] OperateWelfareMgr::Init...nullptr == pLoginMap...fail");
	}
	
	return true;
}

bool OperateWelfareMgr::UnInit()
{
	for(auto it = mConfigMap.begin(); it != mConfigMap.end(); it++)
	{
		for (int i = 0; i < (int)it->second->GetVecWelfareItem().size(); i++)
		{
			delete it->second->GetVecWelfareItem()[i];
		}
		it->second->GetVecWelfareItem().clear();
		delete it->second;
	}
	mConfigMap.clear();
	g_GetTimerAxis()->KillAllFixTimer(this);
	g_GetTimerAxis()->KillAllTimer(this);
	return true;
}

bool OperateWelfareMgr::Update(uint64_t tick)
{
	return true;
}

const ExchangeRewardCfg* OperateWelfareMgr::GetExchangeRewardConfig(uint32_t indexId) const
{
	auto iter = mMapExchangeRewardCfg.find(indexId);
	if (iter != mMapExchangeRewardCfg.end())
	{
		return &iter->second;
	}
	return nullptr;
}

bool OperateWelfareMgr::IsSpecialExchangeItemId(uint32_t item) const
{
	if (mSetSpecialExchangeItems.find(item) != mSetSpecialExchangeItems.end())
	{
		return true;
	}
	return false;
}

const OperateActConfigItem* OperateWelfareMgr::GetWelfareConfigItem(uint32_t ActId)
{
	auto it = mConfigMap.find(ActId);
	if (it != mConfigMap.end())
	{
		return it->second;
	}
	return nullptr;
}

OperateActConfigItem* OperateWelfareMgr::GetWelfareConfigItemFromActType(uint32_t ActType)
{
	for(auto it = mConfigMap.begin(); it != mConfigMap.end(); it++)
	{
		if (it->second->GetActType() == ActType)
		{
			return it->second;
		}
	}
	return nullptr;
}

std::pair<uint32_t, uint32_t>* OperateWelfareMgr::GetActIdItemIdFromRechargeId(uint32_t rechargeId)
{
	auto it = mMapRechargeIdActIdItemId.find(rechargeId);
	if (it != mMapRechargeIdActIdItemId.end())
	{
		return &it->second;
	}
	return nullptr;
}

const WelfareConfigItem* OperateWelfareMgr::GetWelfareConfigItem(uint32_t ActId, uint32_t ItemId)
{
	const OperateActConfigItem* pActItem = GetWelfareConfigItem(ActId);
	if (pActItem)
	{
		for(int i = 0; i < (int)pActItem->GetVecWelfareItem().size(); i++)
		{
			const WelfareConfigItem* item = pActItem->GetVecWelfareItem()[i];
			if (item->GetItemId() == ItemId)
			{
				return item;
			}
		}
	}

	return nullptr;
}

const MAP_SINGLE_GOALS7DAYS* OperateWelfareMgr::GetSingleGoals7Data(uint32_t targetType)	const
{
	MAP_ALL_GOALS7DAYS::const_iterator iter = mGoal7DaysMap.find(targetType);
	if (iter != mGoal7DaysMap.end())
	{
		return &iter->second;
	}
	return nullptr;
}


